<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of TypeController
 *
 * @author Hoan Vu
 */
class Admin_TypeController extends Zend_Controller_Action {
    function indexAction() {
        $typeDao = new TypeDAO();
        $this->view->typeList = $typeDao->getTypeList();
    }

    function addAction() {
        $typeDao = new TypeDAO();

        if ($this->_request->isPost()) {
            $name = $this->_request->getParam('name');
            $description = $this->_request->getParam('description');
            $discount = $this->_request->getParam('discount');
            $data = array(
                'name' => $name,
                'description' => $description,
                'discount' => $discount,
                'status' => 1,
            );
            $typeDao->addType($data);
            $this->_redirect('admin/type');
        }
    }

    function updateAction() {
        $typeDao = new TypeDAO();

        if ($this->_request->isPost()) {
            $id = $this->_request->getParam('id');
            $name = $this->_request->getParam('name');
            $description = $this->_request->getParam('description');
            $discount = $this->_request->getParam('discount');
            $data = array(
                'id' => $id,
                'name' => $name,
                'description' => $description,
                'discount' => $discount,
                'status' => 1,
            );
            $typeDao->editType($data);

            $this->_redirect('/admin/type');
        } else {
            $id = (int)$this->_request->getParam('id', 0);

            if ($id > 0) {
                $this->view->oldType = $typeDao->getType($id);
            }
        }
    }

    function deleteAction() {
        $typeDao = new TypeDAO();
        $id = $this->_request->getParam('id');
        $typeDao->deleteType($id);
        $this->_redirect('admin/type');
    }
}
?>
